<?php


namespace collections\set;

use collections\map\TreeMap;

class TreeSet implements Set
{
    /** @var TreeMap */
    private $m;

    /** @var object */
    private static $PRESENT = null;

    /**
     * TreeSet constructor.
     * @param $m TreeMap|null
     */
    public function __construct($comparator = null, TreeMap $m = null) {
        if (is_null($m)) {
            $this->m = new TreeMap($comparator);
        } else {
            $this->m = $m;
        }
        if (is_null(self::$PRESENT)) {
            self::$PRESENT = new \stdClass();
        }
    }

    public function size()
    {
        return $this->m->size();
    }

    public function isEmpty()
    {
        return $this->m->isEmpty();
    }

    public function contains($o)
    {
        return $this->m->containsKey($o);
    }

    public function toArray() {
        $a = [];
        $this->m->each(function($key) use (&$a) {
            array_push($a, $key);
        });
        return $a;
    }

    /**
     * @param $e
     * @return boolean
     */
    public function add($e)
    {
        return $this->m->put($e, self::$PRESENT) == null;
    }

    public function remove($e)
    {
        return $this->m->remove($e);
    }
}